package controller.web;

import business.ObatListing;
import business.Obat;
import business.Admin;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;

public class ObatAddServlet extends Servlet {
    private Admin admin;

    public ObatAddServlet(){
        super();
        setTemplate("/WEB-INF/addObat.jsp");
    }

    @Override
    protected void process() {

        setMessage("");

        if (getRequest().getParameter("act") != null && getRequest().getParameter("act").equals("add")) {
            try {
                if (validate_field()) {
                        HttpSession session = getRequest().getSession();
                        Admin ad = (Admin) session.getAttribute("username");
                        ObatListing dl = new ObatListing();
                        Obat o = dl.getObat(null);
                        if (o == null) {
                            o = new Obat();
                            o.setDeskripsi(getRequest().getParameter("deskripsi"));
                            o.setAdmin(ad);
                            dl.tambahObat(o);
                        }
                } else {
                    getResponse().sendRedirect("dataKosong");
                }
                getResponse().sendRedirect("userHome");
            } catch (IOException ex) {
                Logger.getLogger(ObatAddServlet.class.getName()).log(Level.SEVERE, null, ex);
            }



        }
    }

    boolean validate_field() {

        String merk = getRequest().getParameter("merk");

        if(merk == null){
            return false;
        }

        return true;
    }


}
